<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;
class IndexStore extends BaseModel
{
	//定义表名

	protected $name = 'longbing_card_shop_store';


	protected $append = [

	    'img_text'
    ];

    /**
     * @param $value
     * @param $data
     * @功能说明:转视频
     * @author chenniang
     * @DataTime: 2020-06-24 14:10
     */
	public function getImgTextAttr($value,$data){

	    if(!empty($data['img'])){

	         $data = transImages([$data['img']],['img']);

            return !empty($data[0])?$data[0]:'';

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:46
     * @功能说明:自提点详情
     */
    public function storeInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:29
     * @功能说明:自提列表
     */
    public function storeList($dis,$where,$page,$file = '*',$alh){

        $res = $this->where($dis)
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->field([$file,$alh])
            ->order(['distance asc','id desc'])
            ->paginate($page)
            ->toArray();

        if(!empty($res['data'])){

            foreach ($res['data'] as &$v){

                $v['distance'] = round($v['distance'],2);
            }

        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:29
     * @功能说明:自提列表
     */
    public function storeListV2($dis,$where,$page,$file = 'a.*',$alh,$map=[]){

        $res = $this->alias('a')
            ->join('longbing_card_shop_store_company b','a.id = b.store_id','left')
            ->where($dis)
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->where(function ($query) use ($map){
                $query->whereOr($map);
            })
            ->field([$file,$alh])
            ->group('a.id')
            ->order(['distance asc','a.id desc'])
            ->paginate($page)
            ->toArray();

        if(!empty($res['data'])){

            foreach ($res['data'] as &$v){

                $v['distance'] = round($v['distance'],2);
            }

        }

        return $res;

    }





    /**
     * @author chenniang
     * @DataTime: 2020-07-01 13:22
     * @功能说明:获取最长预约时间
     */
    public function getDayMax($data){
        //最长预约多少天的
        $max_day = $data['max_day'];

        $week    = 0;

        $no_week = 0;

        $all_day = [];
        //计算出每一天的时间搓
        for ($i = $data['delay'];$i<400;$i++){

            $day = time()+86400*$i;
            //查看今天是星期几
            $day_week = date('w',$day);

            if($data['reception_time']==1&&($data['work']==0||$data['no_work']==0)){

                if(($data['work']==1&&$no_week<$max_day)||($data['no_work']==1&&$week<$max_day)){
                    //如果是非工作日
                    if(in_array($day_week,[0,6])&&$i!=0){

                        $week ++;

                    }elseif(!in_array($day_week,[0,6])&&$i!=0){

                        $no_week++;

                    }
                    $all_day[$i]['time'] = $day;

                    $all_day[$i]['id']   = $data['id'];
                    //拼接好的
                    $all_day[$i]['date_text'] = date('m-d',$day).'  ('.changeWeek($day_week).')';

                    $all_day[$i]['status'] = 1;

                    if($data['delay']==1&&$data['work']==1&&$no_week<$data['delay_day']+1){

                        $all_day[$i]['status'] = 0;

                    }

                    if($data['delay']==1&&$data['no_work']==1&&$week<$data['delay_day']+1){

                        $all_day[$i]['status'] = 0;
                    }

                    if((!in_array($day_week,[0,6])&&$data['work']==0)||(in_array($day_week,[0,6])&&$data['no_work']==0)){

                        $all_day[$i]['status'] = 0;
                    }

                }else{

                    break;
                }

            }else{

                $icon = $data['delay']==0?1:0;

                if(count($all_day)<$max_day+$icon){

                    $all_day[$i]['time'] = $day;

                    $all_day[$i]['id']   = $data['id'];
                    //拼接好的
                    $all_day[$i]['date_text'] = date('m-d',$day).'  ('.changeWeek($day_week).')';

                    $all_day[$i]['status'] = 1;

                    if($data['delay']==1&&count($all_day)<=$data['delay_day']){

                        $all_day[$i]['status'] = 0;

                    }

                }else{

                    break;

                }
            }
        }
        return array_values($all_day);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-01 14:42
     * @功能说明:获取真实的延后天数
     */
    public function getDelayDay($data){

        //最长预约多少天的
        $max_day = $data['delay_day'];

        if($data['delay']==0||$data['reception_time']==0){

            return $max_day;
        }
        $week    = 0;

        $no_week = 0;

        $all_day = [];
        //计算出每一天的时间搓
        for ($i = 0;$i<90;$i++){

            $day = time()+86400*$i;
            //查看今天是星期几
            $day_week = date('w',$day);

            if($data['reception_time']==1&&($data['work']==0||$data['no_work']==0)){

                if(($data['work']==1&&$no_week<$max_day)||($data['no_work']==1&&$week<$max_day)){
                    //如果是非工作日
                    if(in_array($day_week,[0,6])){

                        $week ++;

                    }else{

                        $no_week++;

                    }

                    $all_day[$i] = $day;

                }else{

                    break;
                }

            }
        }


    }













}